package com.yaozheng.vocationaltraining.service.impl.exam;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.exam.IPKHallView;
import com.yaozheng.vocationaltraining.iview.exam.IPKMorePersonHallView;
import com.yaozheng.vocationaltraining.service.exam.PKMorePersonHallService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;
import com.yaozheng.vocationaltraining.view.MemoryWealthItemView;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

import java.util.HashMap;

/**
 * Created by moon on 2017/11/16.
 */

@EBean
public class PKMorePersonHallServiceImpl implements PKMorePersonHallService {
    IPKMorePersonHallView iTokenBaseView;
    @Override
    public void init(IPKMorePersonHallView iTokenBaseView) {
        this.iTokenBaseView = iTokenBaseView;
    }

    @Background
    @Override
    public void list(int gameCategoryId) {
        try {
            OkHttpClientManager
                    .getInstance()
                    .get(Constants.ROOTURL + "/pk/multi-player/game-cate/"+gameCategoryId+"/rooms",
                            iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
                                @Override
                                public void responseSuccess(JSONObject jsonObject) {
                                    iTokenBaseView.responseSuccess(jsonObject);
                                }

                                @Override
                                public void responseError(String error) {
                                    iTokenBaseView.responseError(error);
                                }
                            });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .responseError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void partInPK(final int gameId,final int roomId,final int remaining_time) {

        try {
            OkHttpClientManager.getInstance().get(
                    Constants.ROOTURL + "/pk/multi-player/game/"+gameId+"/in",
                    iTokenBaseView.getToken(), new BaseView(iTokenBaseView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iTokenBaseView.parkInPKSuccess(jsonObject,gameId,roomId, remaining_time);
                        }

                        @Override
                        public void responseError(String error) {
                            iTokenBaseView.parkInPKError(error);
                        }
                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .parkInPKError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void launcherPKMorePerson(int gameCategoryId) {

        try {
            HashMap<String, String> para = new HashMap<String, String>();
            para.put("aaaa", "xxxx");
            OkHttpClientManager.getInstance().post(
                    Constants.ROOTURL + "/pk/multi-player/game-cate/"+gameCategoryId+"/game",
                    iTokenBaseView.getToken(), para, new BaseView(iTokenBaseView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iTokenBaseView.launcherPKMorePersonSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iTokenBaseView.launcherPKMorePersonPKError(error);
                        }
                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iTokenBaseView.isResponseResult()) {
                iTokenBaseView
                        .launcherPKMorePersonPKError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }
}
